<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 后台密码加密方法
 * @param  string $password [description]
 * @param  string $salt     [description]
 * @return string           [description]
 */
function encryptAdminPassword($password, $salt = '')
{
    return md5(md5($password). $salt);
}

function getDomain()
{
    $secure = is_ssl();
    if (isset($_SERVER['HTTP_HOST'])) {
        $domain = $_SERVER['HTTP_HOST'];
    } else {
        $domain = $_SERVER['SERVER_NAME'];
        $port = $secure
            ? (isset($_SERVER['SERVER_PORT']) ? (int)$_SERVER['SERVER_PORT'] : 443)
            : (isset($_SERVER['SERVER_PORT']) ? (int)$_SERVER['SERVER_PORT'] : 80);
        if (($port !== 80 && !$secure) || ($port !== 443 && $secure)) {
            $domain .= ':' . $port;
        }
    }
    return $domain;
}

/**
 * 判断是否SSL协议
 * @return boolean
 */
function is_ssl() {
    if(isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))){
        return true;
    }elseif(isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'] )) {
        return true;
    }
    return false;
}

/**
 * 获取当前域名
 */
function getHost()
{
    $secure = is_ssl();
    $http = $secure ? 'https' : 'http';
    $host = $http . '://' . getDomain();
    return $host;
}

/**
 * 获取当前完整url地址
 */
function getCurUrl()
{
    $host = getHost();
    $url = $host . $_SERVER ['REQUEST_URI'];
    return $url;
}

/**
 * json返回数据公共封装方法
 * @param  [type]      $data   [description]
 * @param  int|integer $status [description]
 * @return [type]              [description]
 */
function jsonResult($data, int $status=1, $url = '')
{
    return json([
        'data' => $data,
        'status' => $status,
        'url' => $url
    ]);
}


/**
 * 获取统计起始到结束的时间每一天戳数组
 */
function getBeginAndEndStamps($date)
{
    if (empty($date)) {
        $start_time = strtotime(date('Y-m-d 00:00:00', time()));
        $end_time = ($start_time + 86400 - 1);
        $start_time = $start_time - (6 * 24 * 60 * 60);
    } else {
        $start_time = strtotime(date('Y-m-d 00:00:00', strtotime($date[0])));
        $end_time = strtotime(date('Y-m-d 23:59:59', strtotime($date[1])));
    }
    $stamp = [];
    while ($start_time + (24 * 60 * 60) <= $end_time + 1) {
        $str_time = date('Y-m-d', $start_time);
        $stamp[$str_time] = [$start_time, ($start_time + (24 * 60 * 60)) - 1];
        $start_time = $start_time + (24 * 60 * 60);
    }
    return $stamp;
}


/**
 * 获取统计起始到结束的时间头尾两天天戳数组
 */
function getBeginAndEndStamp($date)
{
    if (empty($date)) {
        //        $start_time = strtotime(date('Y-m-d 00:00:00', time()));
        //        $end_time = ($start_time + 86400 - 1);
        //        $start_time = $start_time - (6 * 24 * 60 * 60);
        $start_time = 0;
        $end_time = 0;
    } else {
        $start_time = strtotime(date('Y-m-d 00:00:00', strtotime($date[0])));
        $end_time = strtotime(date('Y-m-d 23:59:59', strtotime($date[1])));
    }
    return [
        'begin' => $start_time,
        'end' => $end_time
    ];
}

/**
 * 获取分页limit
 * @param  integer $pageSize 一页显示几条数据
 * @return string            分页limit，如 '0, 10'
 */
function getLimit($pageSize = 20) {
    $p = request()->param('page', '1', 'intval');
    if ($p <= 0 ) {
        $p = 1;
    }
    $start = ($p - 1) * $pageSize;
    return $limit = $start . ',' . $pageSize;
}

//创建订单号
function create_sn($type=1)
{
    $str=date("YmdHis",time()).rand(10,99);
    if ($type==1){
        $order_sn=model("DingzhiOrder")->where(['order_sn'=>$str])->field('id')->find();
    }elseif ($type==2){
        $order_sn=model("Dingzhi")->where(['order_sn'=>$str])->field('id')->find();

    }
    if (!empty($order_sn)){
        $str=create_sn();
    }else{
        return $str;
    }
}

function plan_sn()
{
    $str=rand(100000,999999);
    $plan_sn=model("DingzhiPlan")->where(['plan_sn'=>$str])->field('id')->find();
    if (!empty($order_sn)){
        $str=plan_sn();
    }else{
        return $str;
    }
}

/*
* 判断是否为微信浏览器
*/
function isWeixinBrowser()
{
    $agent = request()->header('user-agent');
    if (strpos($agent, "MicroMessenger") == false) {
        return false;
    }
    return true;
}

function throwException($type = 'api', $msg, $code = 0)
{
    $e = $type == 'api' ? 'app\common\exception\InterfaceException' : '\think\Exception';
    throw new $e($msg, $code);
}
function trimAll($str, $length = 0) {
    $str = strip_tags(preg_replace('/style=".*?"/i','',trim($str)));
    if ($length > 0) {
        $str = mb_substr($str, 0, $length);
    }
    return $str;
}
